package com.googlecode.gunncs.vis;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import com.googlecode.grtframework.core.GRTLoggedProcess;
import com.googlecode.grtframework.vis.Displayable;
import com.googlecode.grtframework.vis.Displayer;
import com.googlecode.grtframework.vis.IMountedPosition;
import com.googlecode.grtframework.vis.Mountable;

/**
 * Tool for debugging MountedPositions
 * 
 * @author ajc
 * 
 */
public class MountVisual extends GRTLoggedProcess implements Mountable,
		Displayable, MouseMotionListener, MouseWheelListener {

	private final IMountedPosition mount;
	private final Displayer d;

	public MountVisual(Displayer d, IMountedPosition mount, String name) {
		super(name);
		this.d = d;
		this.mount = mount;
	}

	@Override
	public double getHeading() {
		return mount.getHeading();
	}

	@Override
	public int getX() {
		return mount.getX();
	}

	@Override
	public int getY() {
		return mount.getY();
	}

	@Override
	public void display(Graphics g) {

		int x = getX();
		int y = getY();
		// draw arrow to self
		drawArrow(g, mount.getParents()[0].getX(),
				mount.getParents()[0].getY(), x, y);

		// draw self
		g.setColor(Color.green);
		g.fillRect(getX(), getY(), 5, 5);

		g.setColor(Color.black);
		double r = mount.getR();
		double theta = mount.getTheta();
		g.drawString(name, x + 0, y + 20);
		g.drawString(
				("polar: \t(" + r + ",  " + mount.getTheta() / Math.PI + "pi)"),
				x + 0, y + 30);
		g.drawString(("rect: \t(" + (int) (r * Math.cos(theta)) + ",  "
				+ (int) (r * Math.cos(theta)) + ")"), x + 0, y + 40);

		g.drawString(("absRect: \t(" + x + ",  " + y + ")"), x + 0, y + 50);

		// heading
		g.setColor(Color.BLACK);
		int headingx = (int) (50 * Math.cos(getHeading())) + x;
		int headingy = (int) (50 * Math.sin(getHeading())) + y;
		g.drawLine(x, y, headingx, headingy);

		g.setColor(Color.GRAY);
		g.drawString("rel:" + mount.getHeadingRelative() / Math.PI + "pi)",
				headingx, headingy);
		g.drawString("abs:" + getHeading() / Math.PI + "pi)", headingx,
				headingy + 10);

		// System.out.println(getHeading());
		// g.drawRect(getX(), getY(), 10, 10);
		// g.drawLine(getX(), getY(), x2, y2)
	}

	private void drawArrow(Graphics g, int x, int y, int x2, int y2) {
		g.setColor(Color.black);
		g.fillRect(x, y, 5, 5);

		g.setColor(Color.gray);
		g.drawLine(x, y, x2, y2);

		g.setColor(Color.green);
		g.fillRect(x2, y2, 5, 5);
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		mount.setPositionAbsolute(arg0.getX(), arg0.getY());
		// mount.setPositionRelative(arg0.getX(), arg0.getY());
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		// System.out.println(arg0.getClickCount());
		mount.setHeadingRelative(arg0.getClickCount() / 20.0);
		// TODO Auto-generated method stub

	}

	@Override
	public synchronized void initiate() {
		super.initiate();
		d.addDisplayable(this);
	}

	@Override
	public synchronized void terminate() {
		super.terminate();
		d.removeDisplayable(this);
	}

}
